package com.optitronics.controllers;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.optitronics.beans.Product;
import com.optitronics.ui.ComboBoxSupport;
import com.optitronics.util.OptitronicsConstants;

@RequestMapping("/products")
@Controller
@SessionAttributes("product")
public class ProductFormController {

	@Autowired
	private MessageSource messageSource;

	@RequestMapping(value = "/new/{purpose}", method = RequestMethod.GET)
	public String createForm(@PathVariable("purpose") Integer purpose, Model model) {

		Product product = new Product();
		product.setPurpose(purpose);
		product.setTaxIncludedInPrice(Product.PURPOSE_PERSONS.equals(purpose));
		product.setActive(Boolean.TRUE);

		model.addAttribute("product", product);
		referenceData(product, model);

		return "products/create";
	}

	@RequestMapping(value = "/{productId}/edit", method = RequestMethod.GET)
	public String updateForm(@PathVariable("productId") Integer productId, Model model) {
		Product product = Product.findProduct(productId);
		model.addAttribute("product", product);
		referenceData(product, model);

		return "products/create";
	}

	private void referenceData(Product product, Model model) {

		String titleKey = product.getId() == null ? "label.product.new" : "label.product.edit";
		model.addAttribute("titleKey", titleKey);

		Collection<ComboBoxSupport> purposeses = new ArrayList<ComboBoxSupport>();

		purposeses.add(new ComboBoxSupport(Product.PURPOSE_PERSONS, messageSource.getMessage("label.products.purpose."
				+ Product.PURPOSE_PERSONS, null, LocaleContextHolder.getLocale())));
		purposeses.add(new ComboBoxSupport(Product.PURPOSE_COMPANIES, messageSource.getMessage(
				"label.products.purpose." + Product.PURPOSE_COMPANIES, null, LocaleContextHolder.getLocale())));

		model.addAttribute("purposeses", purposeses);

		model.addAttribute("PURPOSE_PERSONS", Product.PURPOSE_PERSONS);
		model.addAttribute("PURPOSE_COMPANIES", Product.PURPOSE_COMPANIES);

		model.addAttribute("PRODUCT_COMPANY_RENEWAL_PERCENT", OptitronicsConstants.PRODUCT_COMPANY_RENEWAL_PERCENT);
		model.addAttribute("PRODUCT_COMPANY_ACADEMIC_PERCENT", OptitronicsConstants.PRODUCT_COMPANY_ACADEMIC_PERCENT);
	}

	@RequestMapping(method = RequestMethod.POST)
	public String create(@Valid Product product, BindingResult result, Model model, HttpServletRequest request,
			SessionStatus status) {

		if (result.hasErrors()) {
			model.addAttribute("product", product);
			return "products/create";
		} else {
			status.setComplete();

			if (product.getId() == null) {
				product.persist();
			} else {
				product.merge();
			}
			return "redirect:/products/offers/" + product.getPurpose() + "?page=1&size="
					+ OptitronicsConstants.PAGE_SIZE;
		}
	}

}
